/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp.goal;

import jade.core.AID;

import java.awt.Component;

import javax.swing.JPanel;

import ca.usherbrooke.agent.WorldUpdaterAgent;
import ca.usherbrooke.behaviours.goal.request.RequestAddGoal;
import ca.usherbrooke.behaviours.goal.request.RequestAddGoal.AddGoalInfo;
import ca.usherbrooke.mainApp.MainAppAgent;
import ca.usherbrooke.mainApp.view.goal.PanelGoals;
import ca.usherbrooke.mainApp.view.simulation.PanelSimulation;
import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class GoalAgentBuilderViewer extends WorldUpdaterAgent {
	private static final long serialVersionUID = 1L;

	private AID goalAgent;

	@Override
	protected void setup() {
		super.setup();
		goalAgent = (AID) getArguments()[this.argumentsIdx + 0];
	}

	@Override
	protected void initWorld() {
		JPanel pane = null;
		while (pane == null) {
			pane = this.getGoalPanel();

			if (pane != null) {
				pane.add(new PanelGoals(this));
			}
		}
	}

	public IWorldModel getWorld() {
		return this.world;
	}

	public void addGoal(AddGoalInfo goal) {
		RequestAddGoal request = new RequestAddGoal(this, goalAgent, goal);
		this.addBehaviour(request);
	}

	protected JPanel getGoalPanel() {
		JPanel panel = null;

		Component[] comps = MainAppAgent.APP_FRAME.getMainPanel()
				.getCenterPanel().getComponents();

		for (Component component : comps) {
			if (component instanceof PanelSimulation) {
				panel = ((PanelSimulation) component).getGoalPanel();
			}
		}
		return panel;
	}
}
